{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8e773469-72bf-4249-a0e0-fc101f5556a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import xlwings as xw\n",
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ca2c1e06-4bc6-44fe-83da-197cc7c3a836",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取要追加的行数据，以列表嵌套\n",
    "df_rows = pd.read_excel('./excel_output.xls',header=0,usecols=[1,2,3,4,5,6,7,8,9])\n",
    "list_rows = np.array(df_rows).tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6a9b0900-893f-44a6-be53-73c59134ecdd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 追加数据\n",
    "def append_data(placeholder,list_rows,num):\n",
    "    app = xw.apps.add()\n",
    "    workbook = app.books.open('./销售跟踪try.xlsx')\n",
    "    for i,n in zip(workbook.sheets,range(len(list_rows ))):\n",
    "        worksheet = workbook.sheets[i]\n",
    "        values = worksheet.range(placeholder).expand()\n",
    "        number = values.shape[0]\n",
    "        worksheet.range(number + 1,num).value = list_rows[n]\n",
    "        workbook.save()\n",
    "    workbook.close()\n",
    "    app.quit()\n",
    "    print('Done !!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fb864d5c-8da5-44ba-b1e5-8a78d800dca1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Done !!\n"
     ]
    }
   ],
   "source": [
    "placeholder = 'C1'\n",
    "num = 3\n",
    "append_data(placeholder,list_rows,num)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
